from .. import db
from flask_login import UserMixin
from flask_sqlalchemy import declarative_base

class User(db.Model, UserMixin):
    __tablename__ = 'user'
    id = db.Column('id', db.Integer, primary_key=True)
    openid = db.Column(db.String(255))
    username = db.Column(db.String(255))
    password = db.Column(db.String(255))
    wx_info = db.Column(db.Text)
    status = db.Column(db.Integer, default=1)
    nickname = db.Column(db.String(255))
    qq = db.Column(db.String(255))
    phone = db.Column(db.String(255))
    intro = db.Column(db.String(255))
    avatar = db.Column(db.String(255))
    email = db.Column(db.String(255))
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
    role = db.relationship('Role', lazy="select")
    number = db.Column(db.Integer)

    def is_authenticated(self):
        return True

    def is_actice(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return "1"


class LoginCode(db.Model):
    __tablename__ = 'login_code'
    id = db.Column('id', db.Integer, primary_key=True)
    openid = db.Column(db.String(255), nullable=True)
    qrcode = db.Column(db.String(255))
    uuid = db.Column(db.String(255))
    create_at = db.Column(db.String)
    status = db.Column(db.Integer, default=0)


class Role(db.Model):
    __tablename__ = 'role'
    id = db.Column('id', db.Integer, primary_key=True)
    name = db.Column(db.String(255))
    desc = db.Column(db.String(255))
    status = db.Column(db.BigInteger, default=1)
    menus = db.relationship('RoleMenu', lazy="dynamic")


class RoleMenu(db.Model):
    __tablename__ = 'role_menu'
    id = db.Column(db.Integer, primary_key=True)
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
    menu_id = db.Column(db.Integer, db.ForeignKey('menu.id'))



class Menu(db.Model):
    __tablename__ = 'menu'
    id = db.Column('id', db.Integer, primary_key=True)
    name = db.Column(db.String)
    endpoint = db.Column(db.String)
    endpoint_prefix = db.Column(db.String)
    parent_id = db.Column(db.Integer, db.ForeignKey('menu.id'))
    child = db.relationship('Menu', lazy="dynamic")
